/*
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 * 
 * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
 *
 * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
 * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
 *
 * Contributor(s): 
 *  Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
 *  
 * If you didn't download this code from the following link, you should check
 * if you aren't using an obsolete version: http://www.isqlviewer.com
 */
package org.isqlviewer.event;

import java.sql.Connection;
import java.util.EventObject;

/**
 * Standard event object for notifying actions of the DatabaseConnection.
 * <p>
 * This event object more or less works much like the WindowEvent object where there is an ID that correlates to the
 * type of event is being propagated.
 * <p>
 * Most if not all the time the source of these events should be an instance of DatabaseConnection to help determine
 * which connection these events are coming from.
 * 
 * @see org.isqlviewer.event.DatabaseListener
 * @author Mark A. Kobold &lt;mkobold at isqlviewer dot com&gt;
 * @version 1.0
 */
public class DatabaseEvent extends EventObject {

    private static final long serialVersionUID = 6586214394143524988L;
    /**
     * Inidcates a disconnect from the database.
     */
    public final static int DISCONNECT = 0;
    /**
     * Indicates that a commit has succesfully occured.
     */
    public final static int COMMIT = 1;
    /**
     * Indicates a sucesful rollback has occured.
     */
    public final static int ROLLBACK = 2;
    /**
     * Indicates that the database connection has succesfuly initialized.
     */
    public final static int INITIALIZE = 3;
    /**
     * Indicates that the auto-commite state has changed.
     */
    public final static int AUTO_COMMIT_CHANGE = 4;
    /**
     * For when the catalog is manually updated.
     */
    public final static int CATALOG_CHANGE = 5;

    private int eventCode = -1;

    /**
     * Default event constructor.
     * <p>
     * Basic event constructor with the event code. the code will be checked for validity if not a
     * IllegalArgumentException will be thrown.
     * 
     * @throws IllegalArgumentException if code is invalid.
     * @param source of the event.
     * @param code for this event.
     */
    public DatabaseEvent(Object source, int code) {

        super(source);
        if (!(source instanceof Connection)) {
            throw new IllegalArgumentException();
        }
        checkCode(code);
        eventCode = code;
    }

    /**
     * Returns the code for this event.
     * <p>
     * This value will always be some constant value declared in this class.
     * 
     * @return int representing the event.
     */
    public int getID() {

        return eventCode;
    }

    public Connection getConnection() {

        return (Connection) getSource();
    }

    protected static void checkCode(int code) throws IllegalArgumentException {

        switch (code) {
            case DISCONNECT :
            case COMMIT :
            case ROLLBACK :
            case CATALOG_CHANGE :
            case INITIALIZE :
            case AUTO_COMMIT_CHANGE :
                return;
            default :
                throw new IllegalArgumentException(Integer.toString(code));
        }
    }

}
